package controller;

import com.alibaba.fastjson.JSON;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.*;
import service.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manager")
public class Backend_userController {
    private Logger logger=Logger.getLogger(Backend_userController.class);
    //@Autowired
    @Resource
    private Backend_userService backendUserService;
    @Resource
    private Data_dictionaryService dataDictionaryService;
    @Resource
    private App_categoryService appCategoryService;
    @Resource
    private App_infoService appInfoService;
    @Resource
    private App_versionService appVersionService;
    @RequestMapping("/toLAAAAAAogin")
    public String toLogin(){
        return "backendlogin";
    }

    @RequestMapping("/login")
    public String getUser(@RequestParam String userCode, @RequestParam String userPassword, HttpSession session){
        if (backendUserService.userCode(userCode) == 0) {
            throw new RuntimeException("用户名不存在！");
        } else if (backendUserService.userPassword(userPassword) == 0) {
            throw new RuntimeException("密码输入错误！");
        }else{
            Backend_user user = backendUserService.getUser(userCode, userPassword);
            session.setAttribute("userSession",user);
        }
        return "/backend/main";
    }
    //注销
    @RequestMapping(value = "/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/index.jsp";
    }
    //跳转到手游审核页面
    @RequestMapping("/backend/app/list")
    public String toAppList(Model model,@RequestParam(required = false)String querySoftwareName, @RequestParam(required = false)String queryFlatformId, @RequestParam(required = false)String queryCategoryLevel1,
                            @RequestParam(required = false)String queryCategoryLevel2, @RequestParam(required = false) String queryCategoryLevel3, @RequestParam(defaultValue = "1") String pageIndex){
        List<App_info> list=null;
        if(queryFlatformId==null || queryFlatformId==""){
            queryFlatformId="0";
        }
        if(queryCategoryLevel1==null || queryCategoryLevel1==""){
            queryCategoryLevel1="0";
        }
        if(queryCategoryLevel2==null || queryCategoryLevel2==""){
            queryCategoryLevel2="0";
        }
        if(queryCategoryLevel3==null || queryCategoryLevel3==""){
            queryCategoryLevel3="0";
        }
        Page page=new Page();
        page.setCurrentPageNo(Integer.parseInt(pageIndex));//当前页面
        page.setPageSize(5);		//页面大小
        backendUserService.fy(page,querySoftwareName,Integer.parseInt(queryFlatformId),Integer.parseInt(queryCategoryLevel1),Integer.parseInt(queryCategoryLevel2),
                Integer.parseInt(queryCategoryLevel3));
        list=page.getList();
        List<Data_dictionary> list2=dataDictionaryService.getvalueNameBytypeCode();//所有平台
        model.addAttribute("categoryLevel1List",appCategoryService.getByParentId(1));
        model.addAttribute("categoryLevel2List",appCategoryService.getByParentId(2));
        model.addAttribute("categoryLevel3List",appCategoryService.getByParentId(3));
//        List<App_category> list3=appCategoryService.list();//查询一级分类
//        List<App_category> list4=appCategoryService.list2();//查询二级分类
//        List<App_category> list5=appCategoryService.list3();//查询二级分类
//        model.addAttribute("categoryLevel3List",list5);
//        model.addAttribute("categoryLevel2List",list4);
//        model.addAttribute("categoryLevel1List",list3);
        model.addAttribute("flatFormList",list2);
        model.addAttribute("pages", page);
        model.addAttribute("appInfoList", list);
        model.addAttribute("querySoftwareName",querySoftwareName);
        model.addAttribute("queryFlatformId",Integer.parseInt(queryFlatformId));
        model.addAttribute("queryCategoryLevel1",Integer.parseInt(queryCategoryLevel1));
        model.addAttribute("queryCategoryLevel2",Integer.parseInt(queryCategoryLevel2));
        model.addAttribute("queryCategoryLevel3",Integer.parseInt(queryCategoryLevel3));
//        model.addAttribute("currPage",currPage);
        return "backend/applist";
    }

    @ResponseBody
    @RequestMapping("/byPagrentId")
    public Object byPagrentId(@RequestParam String pid){
        List<App_category> list = appCategoryService.getByParentId(Integer.parseInt(pid));
        return JSON.toJSONString(list);

    }
    @RequestMapping("/backend/app/check")
    public String toCheck(Model model,@RequestParam Integer aid,@RequestParam Integer vid){
        App_info list=appInfoService.getListById(aid);
        model.addAttribute("appInfo",list);
        App_version getListById = appVersionService.getNewById(aid);
        model.addAttribute("appVersion",getListById);
        model.addAttribute("appId",aid);
        System.out.println("准备跳到审核页面了");
        return "/backend/appcheck";
    }

    @RequestMapping("/checksave")
    public String shenHe(Integer id,Integer status){
        //修改手游状态
        appInfoService.UpdateById(id,status);
        return "redirect:/manager/backend/app/list";
    }
}
